package com.buaa.util.etl;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import com.buaa.util.FilterUtils;
import com.buaa.util.SymbolUtils;

public class InstitutionUtils {

	private String institution_path = "D:/1.xlsx";

	/**
	 * 机构编码(1)
	 * 
	 * @param code
	 * @return
	 */
	public static Long getCode(String code) {
		if (code == null || code.equals(""))
			return -1L;
		String regEx = "^\\d{13}$";
		return FilterUtils.longPatternFilter(code, regEx, -1L);
	}

	/**
	 * 单位名称(3)
	 * 
	 * @param name
	 * @return
	 */
	public static String getName(String name) {
		if (name == null || name.equals(""))
			return "";
		name = name.replace("0", "零").replace(".", "");
		Set<Character> symbol = new HashSet<Character>();
		symbol.add('“');
		symbol.add('”');
		symbol.add('《');
		symbol.add('》');
		symbol.add('(');
		symbol.add(')');
		return FilterUtils.characterFilter(name, symbol, "", 5);
	}

	/**
	 * 罗列所有”机构类型“
	 * 
	 * @throws Exception
	 */
	@Test
	public void getAllType() throws Exception {
		InputStream ExcelFileToRead = new FileInputStream(institution_path);
		XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
		XSSFSheet sheet = wb.getSheetAt(0);
		XSSFRow row;
		XSSFCell cell = null;
		String data;
		Set<String> dataSet = new HashSet<String>();
		int cur = 1;

		Iterator<Row> rows = sheet.rowIterator();
		while (rows.hasNext()) {
			row = (XSSFRow) rows.next();
			if (cur == 1) {
				cur = 0;
				continue;
			}
			Iterator<Cell> cells = row.cellIterator();
			for (int i = 0; i < 4; i++)
				cell = (XSSFCell) cells.next();
			data = cell.getStringCellValue();
			if (data != null && data != "" && !dataSet.contains(data))
				dataSet.add(data);
		}
		wb.close();
		System.out.println("总共有" + dataSet.size() + "个");
		for (String string : dataSet) {
			System.out.println(string);
		}
	}

	/**
	 * 机构类型(4)
	 * 
	 * @param name
	 * @return
	 */
	public static String getType(String name) {
		switch (name) {
		case "创新载体":
			return "A";
		case "检测单位联盟":
			return "A001";
		case "孵化器":
			return "A002";
		case "大学科技园":
		case "大学":
			return "A003";
		case "高新区":
			return "A004";
		case "产业基地":
			return "A005";
		case "科研机构":
			return "B";
		case "中试基地":
			return "B001";
		case "重点实验室":
			return "B002";
		case "检测单位":
			return "B003";
		case "企业研发中心":
			return "B004";
		case "高等院校":
			return "B005";
		case "科研院所":
			return "B006";
		case "工程技术中心":
		case "工程技术研究中心":
			return "B007";
		case "生产力促进中心":
			return "B008";
		case "其他":
			return "B009";
		case "企业":
			return "C";
		case "高新技术企业":
			return "C001";
		case "加工制造企业":
		case "制造企业":
			return "C002";
		case "科技型企业":
			return "C003";
		case "科技服务企业":
			return "C004";
		case "其他企业":
			return "C005";
		case "地市管理单位":
			return "D";
		default:
			return "ERR"; // 其他（过滤掉不合法的机构类型，例如“0”、“机构类型”等）
		}
	}

	/**
	 * 机构地址(5)
	 * 
	 * @param addr
	 * @return
	 */
	public static String getAddr(String addr) {
		if (addr == null || addr.equals(""))
			return "";
		addr = addr.replace("#", "号").replace(" ", "");
		// if(addr.contains("黑龙江省"))
		// addr = addr.substring(4);
		// if(addr.contains("黑龙江"))
		// addr = addr.substring(3);
		// if(addr.startsWith("哈尔滨")){
		// if(addr.charAt(3)!='市');
		// //addr = addr.substring(beginIndex, endIndex)
		// }
		Set<Character> symbol = new HashSet<Character>();
		SymbolUtils.addArabicSym(symbol);
		symbol.add('、');
		symbol.add('-');
		symbol.add('(');
		symbol.add(')');
		return FilterUtils.characterFilter(addr, symbol, "", 4);
	}

	/**
	 * 所在省份(6)
	 * 
	 * @param province
	 * @return
	 */
	public static String getProvince(String province) {
		return "黑龙江省";
	}

	/**
	 * 所在地市(7)
	 * 
	 * @param city
	 * @return
	 */
	public static String getCity(String city) {
		if (city == null || city.equals(""))
			return "";
		// 哈尔滨市-齐齐哈尔市-鹤岗市-双鸭山市-鸡西市-大庆市
		// 伊春市- 牡丹江市-佳木斯市-七台河市-黑河市-绥化市-大兴安岭地区
		// 其他（我理解是俄罗斯）
		city = city.trim();
		if (city.startsWith("大兴安岭")) {
			city = "大兴安岭地区";
		} else if (!city.endsWith("市"))
			city += "市";
		return FilterUtils.characterFilter(city, null, "", 0);
	}

	/**
	 * 所在区县(8)
	 * 
	 * @param country
	 * @return
	 */
	public static String getCountry(String country) {
		if (country == null || country.equals(""))
			return "";
		if (!country.endsWith("区") && !country.endsWith("县"))
			country += "区";
		return FilterUtils.characterFilter(country, null, "", 0);
	}

	/**
	 * 所在街道(9)
	 * 
	 * @param country
	 * @return
	 */
	public static String getStreet(String street) {
		if (street == null || street.equals(""))
			return "";
		street = street.replace("#", "号");
		Set<Character> symbol = new HashSet<Character>();
		SymbolUtils.addArabicSym(symbol);
		symbol.add('、');
		symbol.add('-');
		symbol.add('(');
		symbol.add(')');
		return FilterUtils.characterFilter(street, symbol, "", 4);
	}

	/**
	 * 上级主管部门(10)
	 * 
	 * @param name
	 * @return
	 */
	public static Integer getDepart(String departId) {
		if (departId == null || departId.equals(""))
			return -1;
		return Integer.parseInt(departId);
	}

	/**
	 * 法人姓名(11)/联系人姓名(13)
	 * 
	 * @param name
	 * @return
	 */
	public static String getCor_name(String name) {
		if (name == null || name.equals(""))
			return "";
		name = name.replace(" ", "");
		Set<Character> symbol = new HashSet<Character>();
		symbol.add('、');
		symbol.add('(');
		symbol.add(')');
		return FilterUtils.characterFilter(name, symbol, "", 1);
	}

	/**
	 * 法人职务(12)/联系人职务(15)
	 * 
	 * @param job
	 * @return
	 */
	public static String getJob(String job) {
		if (job == null || job.equals(""))
			return "";
		job = job.replace("/", "、").replace(" ", "");
		Set<Character> symbol = new HashSet<Character>();
		symbol.add('、');
		symbol.add('(');
		symbol.add(')');
		return FilterUtils.characterFilter(job, symbol, "", 1);
	}

	/**
	 * 联系人电话(14)
	 * 
	 * @param phone
	 * @return
	 */
	public static String getPhone(String phone) {
		if (phone == null || phone.equals(""))
			return "";
		phone = phone.replace("—", "-");
		if (phone.startsWith("86-"))
			phone = phone.substring(3);
		if (phone.equals("0453-8626-5893"))
			phone = "0453-86265893";
		return FilterUtils.phoneFilter(phone, "");
	}

	/**
	 * 单位网址(16)
	 * 
	 * @param url
	 * @return
	 */
	public static String getUrl(String url) {
		if (url == null || url.equals(""))
			return "";
		url = url.replace("？", ".");
		return FilterUtils.urlFilter(url, "");
	}

	/**
	 * 单位简介(17)
	 * 
	 * @param desc
	 * @return
	 */
	public static String getDesc(String desc) {
		if (desc == null || desc.equals(""))
			return "";
		desc = desc.replace("○", "〇").replace("——", "-").replace("——", "-").replace("—", "-").replace("-", "-");
		// Set<Character> symbol = new HashSet<Character>();
		// SymbolUtils.addAllSym(symbol);
		// return FilterUtils.characterFilter(desc, symbol, "", 4);
		return desc.trim();
	}

	/**
	 * 技术领域(父)(18)/(子)(19)
	 * 
	 * @param tech
	 * @return
	 */
	public static String getTech(String tech) {
		if (tech == null || tech.equals(""))
			return "未定义";
		// 技术领域目前只允许全中文
		return FilterUtils.characterFilter(tech, null, "未定义", 0);
	}

	/**
	 * 成立时间(20)
	 * 
	 * @param birth
	 * @return
	 * @throws ParseException
	 */
	public static Date getBirth(String birth) throws ParseException {
		if (birth == null || birth.equals(""))
			return new SimpleDateFormat("yyyy-MM-dd").parse("1000-01-01");
		return FilterUtils.dateFilter(birth, "yyyy-MM-dd");
	}

	/**
	 * 注册资金(21)
	 * 
	 * @param money
	 * @return
	 */
	public static Integer getReg_money(String money) {
		if (money == null || money.equals(""))
			return -1;
		return FilterUtils.integerFilter(money);
	}

	/**
	 * 单位图片（22）
	 * 
	 * @param pic
	 * @return
	 */
	public static String getPic(String pic) {
		if (pic == null || pic.equals(""))
			return "";
		pic = pic.replace(",", ".");
		String[] suffixes = { ".jpg", ".JPG", ".jpeg", ".JPEG", ".gif", ".GIF", ".png", ".bmp" };
		return FilterUtils.fileFilter(pic, suffixes);
	}

}
